package com.liao.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import java.util.Calendar;

public class ClockView extends View {

    private int h;
    private int m;
    private int s;

    private Paint paint, textPaint;

    private Handler handler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(@NonNull Message msg) {
            getTime();
            invalidate();
            handler.sendEmptyMessage(0x000);
            return false;
        }
    });

    public ClockView(Context context) {
        super(context);
        init();
    }

    public ClockView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private void init() {
        paint = new Paint();
        paint.setAntiAlias(true);
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(5);

        textPaint = new Paint();
        textPaint.setAntiAlias(true);
        textPaint.setTextSize(18);
        textPaint.setStrokeWidth(1);
        getTime();
        handler.sendEmptyMessage(0x000);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int modeW = MeasureSpec.getMode(getMeasuredWidth());
        int modeH = MeasureSpec.getMode(getMeasuredHeight());
        int size = 0;
        switch (modeW) {
            case MeasureSpec.AT_MOST:
                if (modeH == MeasureSpec.EXACTLY) {
                    size = getMeasuredHeight();
                } else {
                    size = 300;
                }
                break;
            case MeasureSpec.EXACTLY:
                size = Math.min(getMeasuredWidth(), getMeasuredHeight());
                break;
            case MeasureSpec.UNSPECIFIED:
                size = 300;
                break;
        }
        setMeasuredDimension(size, size);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        paint.setStyle(Paint.Style.STROKE);
        canvas.drawCircle(getWidth() / 2.0f, getHeight() / 2.0f, getWidth() / 2.0f - 10, paint);

        paint.setTextSize(18);
        for (int i = 0; i < 12; i++) {
            canvas.save();
            canvas.rotate(30 * (i + 1), getWidth() / 2.0f, getHeight() / 2.0f);
            canvas.drawLine(getWidth() / 2.0f, 10, getWidth() / 2.0f, 20, paint);
            canvas.drawText(String.valueOf(i + 1), getWidth() / 2.0f - 6, 35, textPaint);
            canvas.restore();
        }

        paint.setColor(Color.RED);
        paint.setStrokeWidth(6);
        canvas.save();
        canvas.rotate(h * 30, getWidth() / 2.0f, getHeight() / 2.0f);
        canvas.drawLine(getWidth() / 2.0f, getHeight() / 2 + getHeight() / 12,
                getWidth() / 2, getHeight() / 2 - getHeight() / 5.0f * 1, paint);
        canvas.restore();

        paint.setColor(Color.BLACK);
        paint.setStrokeWidth(4);
        canvas.save();
        canvas.rotate(m * 6, getWidth() / 2, getHeight() / 2);
        canvas.drawLine(getWidth() / 2.0f, getHeight() / 2 + getHeight() / 12,
                getWidth() / 2, getHeight() / 2 - getHeight() / 10.0f * 3, paint);
        canvas.restore();

        paint.setStrokeWidth(3);
        canvas.save();
        canvas.rotate(s * 6, getWidth() / 2, getHeight() / 2);
        canvas.drawLine(getWidth() / 2.0f, getHeight() / 2 + getHeight() / 12,
                getWidth() / 2, getHeight() / 2 - getHeight() / 5.0f * 2, paint);
        canvas.restore();

        paint.setStyle(Paint.Style.FILL);
        canvas.drawCircle(getWidth() / 2.0f, getHeight() / 2.0f, 10, paint);
        super.onDraw(canvas);
    }

    private void getTime() {
        Calendar calendar = Calendar.getInstance();
        h = calendar.get(Calendar.HOUR);
        m = calendar.get(Calendar.MINUTE);
        s = calendar.get(Calendar.SECOND);
    }
}
